/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 沈小白 2020-07-22 $
 */<template> 
	<!-- 搜索结果 -->
	<view class="box">
		
			<goods-list :hide="ishide" ref="goodslist"></goods-list>
		
		<!-- 搜索栏 -->
		<view class="content" v-if="!ishide">
		
		<view class="search-modal">
			<view class="search-bar">
				<form @submit="submitSearch">
					<input class="search-input" @input="getgoods" name="word" placeholder="搜索商品">

					</input>
					<button class="search-btn" form-type="submit" role="button" aria-disabled="false"> 搜索 </button>
				</form>
			</view>
			<view class="search-hot">
				<view class="hot-title">热门搜索</view>
				<view class="hot-row">
					<block v-for="(item,index) in hotWord" :key="index">
						<view @tap="searchHotWord" class="hot-item" :data-word="item">{{item}}</view>
					</block>
				</view>
			</view>
			<view class="history" v-if="history.length>0">
                <view class="label">历史搜索</view>
                <view class="history-box">
                    <block v-for="(item,index) in history" :key="index">
                        <view class="history-item" @tap.stop="searchHist" :data-index="index">
                            <text>{{item.text}}</text>
                            <view class="del-icon" @tap.stop="delHistory" :data-ind="index">删除</view>
                        </view>
                    </block>
                    
                   
                </view>
            </view>
		</view>
	</view>
	</view>
</template>

<script>
	const globalData=getApp().globalData//从app.vue获取用户信息
	import goodsList from "../goodsList/goodsList.vue";
	import {searchGoods} from "@/api/api.js";
	export default {
		
		data() {
			return {
				 history:"",//历史数据
				ishide:true,//控制goodlsit组件 默认true隐藏
				brandId:[],//商品id
				size:10,//默认10条
				searchName:"",//搜索名称
				hotWord:"",
			};
		},
		components:{
			goodsList,
		},
		watch: {
			history:{
				handler:function(){
					this.saveHistory(this.history)
				}
			}
		},
		onLoad(e){
			if(this.$store.state.config.basic_hot_keywords){
				this.hotWord=this.$store.state.config.basic_hot_keywords.split('|')
			}
			
			this.brandId=e.brand_id;
			/*关键字*/ 
			
			
		},
		onShow(){
			 this.history=this.getHistory()
		},
		mounted(){
			
			if(this.brandId!="undefined"){
				
				this.ishide=false
			}else{
				this.handGood()
			}
			
		},
		methods:{
			handGood(){//执行goodsList组件的方法

				this.$refs.goodslist.getbarand()
				this.ishide=true;
			},
			/*input事件*/ 
			getgoods(e){
				console.log(e)
				let value=e.detail.value
				this.searchName=value
			},
			/*获取历史记录*/
			getHistory(){
				/*获取历史记录*/
				var that =this;
				var data=uni.getStorageSync('history');
				console.log(data)
				return data
			},
			/*缓存 搜索记录*/
			saveHistory(historyData){
				uni.setStorageSync('history', historyData);
			},
			/*缓存 搜索记录*/
			setHistory(obj){
				var historyData=[];
				if(this.getHistory()){
					historyData=this.getHistory()
				}
				if(historyData.lengh==0){
					historyData=(obj);
				}else{
					/*重复字检索*/
					var newArr=[];
					historyData.map((item,index)=>{
						console.log(item)
						if(item.text==obj.text){
							/*删除原来的数据,重新插入到第一个*/
							console.log(index)
							let arr=historyData.splice(index,1) 
						}
					})
					historyData.unshift(obj);
				}
				this.saveHistory(historyData)
			},
			/*删除历史记录*/  
			delHistory(e){
				console.log(e)
				var index =e.currentTarget.dataset.ind;
				this.history.splice(index,1)
			},
			searchHist(e){
				var index=e.currentTarget.dataset.index;
				this.$refs.goodslist.searchName=this.history[index].text; 
				// this.goSearchList()
				// this.searchName=e.currentTarget.dataset.word
				// this.$refs.goodslist.searchName=this.searchName
				this.handGood()
			},
			searchGoods(params){
				searchGoods(params).then(res=>{
					console.log(res)
					if(res){
						/**搜索根据获得的brand_id(默认取第一条,目前所有brand_id都一样) 传给goodslist组件 
						 * */ 
						let brandId=[]
						res.records.forEach((i,index)=>{
							console.log()
							brandId.push(i.brand_id)
						})
						this.brandId=brandId
						this.$refs.goodslist.brandId=res.records.length>0?this.brandId:''
						this.brandId=res.records.length>0?this.brandId:''
						console.log(this.$refs.goodslist.brandId)
						this.handGood()
					}
				})
			},
			/*热词搜索*/
			searchHotWord(e){
				console.log(e)
				this.searchName=e.currentTarget.dataset.word
				this.$refs.goodslist.searchName=this.searchName
				this.handGood()
			}, 
			/*搜索商品*/ 
			submitSearch(){
				 /*缓存 搜索历史*/ 
				var obj={
					text:this.searchName
				}
            	this.setHistory(obj)
				this.$refs.goodslist.searchName=this.searchName
				// this.searchGoods(params)
				this.handGood()
			}
		}
	}
</script>

<style lang="scss">
	page{
		display: flex;
		flex-direction: column;
		height: 100%;
	}
	.box{
		display: flex;
		flex-direction: column;
		height: 100%;
		width: 100%;
	}
	
	.content{
		display: flex;
		height: 100%;
		flex-direction: column;
		width: 100%;
	}
	.container {
		color: #666;
	}

	.nav-item {
		float: left;
		width: 22.5%;
		height: 90rpx;
		line-height: 90rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		border-bottom: 1px solid #f3f3f3;
		background-color: #fff;
	}

	.nav-item .ico-dg {
		width: 18rpx;
		height: 12rpx;
		margin-left: 10rpx;
		line-height: 0;
	}

	.nav-item .ico-filter {
		width: 20rpx;
		height: 20rpx;
		margin-left: 10rpx;
		line-height: 0;
	}

	.ico-dir {
		width: 16rpx;
		height: 22rpx;
		margin-left: 10rpx;
		background-repeat: no-repeat;
		background-size: cover;
		background-position-x: 0;
	}

	.ico-dir-dn {
		background-position-x: -16rpx;
	}

	.ico-dir-up {
		background-position-x: -32rpx;
	}

	.nav-search {
		width: 10%
	}

	.nav-item .search-img {
		width: 30rpx;
		height: 30rpx;
	}

	.choice_list {
		background-color: #fff;
	}

	.choice_item {
		position: relative;
		width: 100%;
		min-height: 200rpx;
		padding: 10rpx 0;
		border-bottom: 1px solid #f3f3f3;
		font-size: 28rpx;
	}

	.img-wrap {
		float: left;
		width: 180rpx;
		height: 180rpx;
		padding: 0 10rpx 10rpx;
		position: relative;
	}

	.Seconds_kill {
		background: #ff3b3b;
		padding: 4rpx 22rpx 4rpx 28rpx;
		text-align: center;
		border-radius: 0 100rpx 100rpx 0;
		color: #fff;
		font-size: 26rpx;
		position: absolute;
		top: 30rpx;
		left: 10rpx;
	}

	.img-wrap image {
		width: 100%;
		height: 100%;
	}

	.item-cont .title {
		height: 72rpx;
		margin: 16rpx 0;
		padding-right: 10rpx;
		line-height: 36rpx;
		color: #333;
		overflow: hidden;
	}

	.price {
		display: inline-block;
		color: #ff3b3b;
		padding-bottom: 20rpx;
		font-size: 30rpx;
		font-weight: 600;
	}

	.price text {
		font-size: 22rpx;
	}

	.comment {
		font-size: 25rpx;
	}

	.carts image {
		width: 44rpx;
		height: 44rpx;
		position: absolute;
		right: 20rpx;
		bottom: 10rpx;
	}

	/* 弹框相关 */
	.filter-modal {
		position: fixed;
		left: 150rpx;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 20;
		background-color: white;
		overflow-x: hidden;
		padding-bottom: 30rpx;
	}

	.filter-box {
		box-sizing: border-box;
		width: 100%;
		font-size: 28rpx;
		padding: 20rpx;
	}

	.filter-name {
		width: 100%;
		padding: 20rpx 0;
		word-break: keep-all;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		font-size: 30rpx;
	}

	.filter-items {
		width: 100%;
	}

	.filter-item {
		float: left;
		width: fit-content;
		max-width: 500rpx;
		word-break: keep-all;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		padding: 10rpx;
		border-radius: 10rpx;
		border: 1rpx solid #f3f3f3;
		margin: 0 10rpx 10rpx 0;
		background-color: #fdfdfd;
		color: #666;
	}

	.viewall-btn {
		margin-top: 20rpx;
		width: 300rpx;
	}

	/* 搜索弹框 */
	.search-modal {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
		z-index: 20;
		background-color: white;
		overflow-x: hidden;
		font-size: 30rpx;
		color: #666555;
	}

	.search-bar {
		width: 100%;
		box-sizing: border-box;
		margin: 15rpx 0;
		padding-left: 45rpx;
		border-bottom: 10rpx solid #f5f5f5;
	}

	.search-input {
		width: 560rpx;
		float: left;
		border: 1rpx solid #f3f3f3;
		box-sizing: border-box;
		height: 60rpx;
		border-radius: 30rpx;
		font-size: 22rpx;
		color: #333232;
		padding-left: 90rpx;
	}

	.search-btn {
		width: 80rpx;
		height: 60rpx;
		background-color: #ff3b3b;
		display: inline-block;
		text-align: center;
		border-radius: 30rpx;
		color: #ffffff;
		font-size: 24rpx;
		margin-left: 25rpx;
	}

	.search-btn .search-img {
		height: 45rpx;
		width: 45rpx;
		padding: 20rpx;
	}

	.search-hot {
		margin-top: 35rpx;
		padding: 0 30rpx;
	}

	.hot-title {
		font-size: 30rpx;
		color: #333;
		margin-bottom: 25rpx;
		font-weight: bold;
	}

	.hot-row {
		display: flex;
		flex-wrap: wrap;
	}

	.hot-item {
		border-radius: 25rpx;
		font-size: 24rpx;
		height: 50rpx;
		line-height: 50rpx;
		padding: 0 24rpx;
		margin-right: 25rpx;
		margin-bottom: 30rpx;
		background-color: #f5f5f5;
		color: #666666;
	}

	.his-title {
		font-size: 30rpx;
		color: black;
		margin-bottom: 25rpx;
		font-weight: bold;
	}

	.search-history {
		padding: 0 30rpx;
	}

	.his-item {
		height: 90rpx;
		border-bottom: 1rpx solid #f3f3f3;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.hi-name {
		font-size: 26rpx;
		color: #666666;
		display: inline-block;
	}

	.hi-del {
		display: inline-block;
		border: 1rpx solid #f3f3f3;
		height: 36rpx;
		font-size: 22rpx;
		border-radius: 18rpx;
		line-height: 35rpx;
		padding: 0 18rpx;
	}

	/* 加规格弹窗 */
	.cover-layer {
		z-index: 20;
	}

	.spec-model {
		position: fixed;
		bottom: 0;
		background: white;
		width: 100%;
		padding: 0 30rpx;
		font-size: 32rpx;
		box-sizing: border-box;
		overflow-x: hidden;
		z-index: 9999;
	}

	.spec-goods {
		padding: 15rpx 0 30rpx;
		float: left;
		width: 100%;
		border-bottom: 1px solid #f3f3f3;
	}

	.spec-img {
		float: left;
		height: 180rpx;
		width: 180rpx;
		margin-top: 16rpx;
	}

	.spec-goods-info {
		float: left;
		padding: 0 25rpx;
		width: 430rpx;
	}

	.spec-goods-name {
		font-size: 30rpx;
		line-height: 35rpx;
		height: 70rpx;
		margin: 15rpx 20rpx 25rpx 0;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;

	}

	.spec-goods-price {
		color: #ff3b3b;
		font-size: 36rpx;
		font-weight: bold;
	}

	.spec-goods-price text {
		font-size: 22rpx;
	}

	.spec-goods-stock {
		margin-top: 10rpx;
		font-size: 26rpx;
		color: #999999;
		font-weight: 500;
	}

	.spec-box {
		width: 100%;
		height: 50rpx;
		overflow: hidden;
		margin-top: 50rpx;
	}

	.spec-name {
		float: left;
		width: 150rpx;
		font-size: 32rpx;
	}

	.spec-btn {
		color: black;
		background-color: #efefef;
		font-size: 24rpx;
		line-height: 48rpx;
		float: left;
		height: 48rpx;
		padding: 0 24rpx;
		border: 1rpx solid #f3f3f3;
		margin: 4rpx 20rpx 4rpx 0;
		border-radius: 24rpx;
		overflow: hidden;
	}

	.spec-btn-click {
		color: #ff3b3b;
		background-color: #ffefef;
		border: 1rpx solid #ff3030;
	}

	.spec_types {
		width: 100%;
		height: auto;
		overflow: hidden;
		padding-top: 20rpx;
	}

	.count {
		float: right;
		width: 184rpx;
		height: 50rpx;
		font-size: 32rpx;
		font-weight: bold;
	}

	.count>view,
	.count>input {
		float: left;
		width: 60rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
	}

	.spec-cart-btns {
		width: 100%;
		line-height: 68rpx;
		margin: 54rpx auto 32rpx;
		height: 68rpx;
	}

	.spec-cart-btn {
		float: left;
		width: 690rpx;
		height: 68rpx;
		font-size: 30rpx;
		text-align: center;
		color: white;
		border-radius: 34rpx;
		line-height: 68rpx;
	}

	.spec-buy {
		background-color: #ff3b3b;
		margin: 54rpx auto 32rpx;
	}
	 .history{
        margin-left: 20rpx;
    }
    .history-box{
        
        .history-item{
            margin: 0 20rpx;
            padding-top: 22rpx;
            padding-bottom: 22rpx;
            display: flex;
            flex-direction: row;
            align-items: center;
            border-bottom: 2rpx solid #F5F5F5;
            text{
                flex: 1;
                color: #999999;
                font-size: 24rpx;
            }
        }
        .del-icon{
            width:84rpx;
            height:36rpx;
            border:2rpx solid rgba(229,229,229,1);
            border-radius:5rpx;
            color: #999999;
            font-size: 24rpx;
            text-align: center;
            line-height: 36rpx;
        }
    }
</style>
